import http from '../http';

// 商品接口类型定义
export interface Product {
  id?: number;
  name: string;
  description?: string;
  categoryId: number;
  brandId: number;
  price: number;
  originalPrice?: number;
  stockQuantity: number;
  status: number; // 0: 下架, 1: 上架, 2: 草稿
  images?: string[];
  specifications?: Record<string, any>;
  weight?: number;
  dimensions?: string;
  tags?: string[];
  seoTitle?: string;
  seoDescription?: string;
  seoKeywords?: string;
  sortOrder: number;
  isRecommended: number; // 0: 否, 1: 是
  isHot: number; // 0: 否, 1: 是
  isNew: number; // 0: 否, 1: 是
  createdAt?: string;
  updatedAt?: string;
  category?: {
    id: number;
    name: string;
  };
  brand?: {
    id: number;
    name: string;
  };
  skus?: ProductSku[];
}

export interface ProductSku {
  id?: number;
  productId: number;
  skuCode: string;
  specifications: Record<string, any>;
  price: number;
  originalPrice?: number;
  stockQuantity: number;
  weight?: number;
  dimensions?: string;
  images?: string[];
  status: number; // 0: 禁用, 1: 启用
  createdAt?: string;
  updatedAt?: string;
}

export interface ProductListParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
  categoryId?: number;
  brandId?: number;
  status?: number;
  isRecommended?: number;
  isHot?: number;
  isNew?: number;
  minPrice?: number;
  maxPrice?: number;
  sortBy?: string;
  sortOrder?: 'ASC' | 'DESC';
}

export interface ProductListResponse {
  list: Product[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export interface ProductStats {
  total: number;
  published: number;
  draft: number;
  offline: number;
  lowStock: number;
  outOfStock: number;
}

export interface ProductSkuListParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
  status?: number;
}

export interface ProductSkuListResponse {
  list: ProductSku[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export class ProductApi {
  /**
   * 创建商品
   */
  createProduct(data: Omit<Product, 'id' | 'createdAt' | 'updatedAt'>) {
    return http.post('/api/products', data);
  }

  /**
   * 获取商品列表
   */
  getProductList(params: ProductListParams = {}) {
    return http.get<{
      code: number;
      message: string;
      data: ProductListResponse;
    }>('/api/products', { params });
  }

  /**
   * 获取商品详情
   */
  getProductDetail(id: number) {
    return http.get<{
      code: number;
      message: string;
      data: Product;
    }>(`/api/products/${id}`);
  }

  /**
   * 更新商品
   */
  updateProduct(id: number, data: Partial<Product>) {
    return http.put(`/api/products/${id}`, data);
  }

  /**
   * 删除商品
   */
  deleteProduct(id: number) {
    return http.delete(`/api/products/${id}`);
  }

  /**
   * 批量删除商品
   */
  batchDeleteProducts(ids: string[]) {
    return http.post('/api/products/batch-delete', { ids });
  }

  /**
   * 更新商品状态
   */
  updateProductStatus(id: number, status: number) {
    return http.patch(`/api/products/${id}/status`, { status });
  }

  /**
   * 获取商品统计数据
   */
  getProductStats() {
    return http.get<{
      code: number;
      message: string;
      data: ProductStats;
    }>('/api/products/stats');
  }

  /**
   * 获取商品SKU列表
   */
  getProductSkus(productId: number, params: ProductSkuListParams = {}) {
    return http.get<{
      code: number;
      message: string;
      data: ProductSkuListResponse;
    }>(`/api/products/${productId}/skus`, { params });
  }

  /**
   * 创建商品SKU
   */
  createProductSku(productId: number, data: Omit<ProductSku, 'id' | 'productId' | 'createdAt' | 'updatedAt'>) {
    return http.post(`/api/products/${productId}/skus`, data);
  }

  /**
   * 更新商品SKU
   */
  updateProductSku(productId: number, skuId: number, data: Partial<ProductSku>) {
    return http.put(`/api/products/${productId}/skus/${skuId}`, data);
  }

  /**
   * 删除商品SKU
   */
  deleteProductSku(productId: number, skuId: number) {
    return http.delete(`/api/products/${productId}/skus/${skuId}`);
  }

  /**
   * 批量删除商品SKU
   */
  batchDeleteProductSkus(productId: number, skuIds: string[]) {
    return http.post(`/api/products/${productId}/skus/batch-delete`, { ids: skuIds });
  }
}

// 导出实例
export const productApi = new ProductApi();